﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Ext.Net;
using System.Web.Security;
using System.Reflection;
using System.Diagnostics;
using System.Web.UI;
using System.Web;

namespace Sio.Mdm.Web.Controls
{
    public class Login : System.Web.UI.Page
    {
        #region Fields
        internal static bool FirstLogin = true;
        internal static string DefaultUrl;
        #endregion

        #region Properties

        #region ScriptManager
        private ResourceManager _manager = null;
        /// <summary>
        /// Get instance of script manager for page
        /// </summary>
        protected ResourceManager ScriptManager
        {
            get
            {
                if (_manager == null)
                {
                    if (ExtNet.ResourceManager == null)
                    {
                        EnsureChildControls();
                        _manager = FindScriptManager();
                    }

                    _manager.RegisterAllResources = true;
                    //_manager.ScriptMode = ScriptMode.Debug;
                }

                return _manager;
            }
        }
        #endregion

        #endregion

        #region Methods

        #region Protected

        #region RedirectFromLogin
        protected void RedirectFromLogin(string userName)
        {
            if (String.IsNullOrEmpty(DefaultUrl))
            {
                string urlRedir = FormsAuthentication.GetRedirectUrl(userName, false);
                ExtNet.Redirect(urlRedir);
            }
            else
            {
                ExtNet.Redirect(DefaultUrl);
            }
        }
        #endregion
        #region RedirectFromLogin
        protected void RedirectFromTipVlogeId(int tipVlogeId)
        {
            if (tipVlogeId == 0) //super admin
                ExtNet.Redirect("Instalacija/");
            else if (tipVlogeId == 1) //navadni admin
                ExtNet.Redirect("Organizacija/");
            else if (tipVlogeId == 4) //pedagoški kader
                ExtNet.Redirect("Organizacija/");
            else if (tipVlogeId == 5) //nepedagoški kader
                ExtNet.Redirect("Organizacija/");
            else
                ExtNet.Redirect("Uporabnik/");

        }
        #endregion

        protected override void OnPreInit(EventArgs e)
        {
            if (!ExtNet.IsAjaxRequest)
            {
                ScriptManager.RegisterClientScriptInclude(
                    "common",
                    ScriptManager.GetWebResourceUrl(
                        typeof(Page),
                        "Sio.Mdm.Web.Controls.scripts.common.js")
                );
            }

            base.OnPreInit(e);
        }

        #region OnLoad
        protected override void OnLoad(EventArgs e)
        {
            // Set timeout in direct events to some reasonable time for debugging
            Ext.Net.ExtNet.Js.Set("Ext.net.DirectEvent.timeout", 180000);
            base.OnLoad(e);
        }
        #endregion

        #region OnPreRender
        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);

            if (ExtNet.IsAjaxRequest)
            {
                return;
            }

            //ExtNet.ResourceManager.RegisterClientInitScript(
            //    "deframe",
            //    String.Format(
            //        "if(top.location != window.location) top.location = '{0}?ReturnUrl={1}';",
            //        FormsAuthentication.LoginUrl,
            //        Server.UrlEncode(FormsAuthentication.DefaultUrl))
            //);

            ExtNet.ResourceManager.RegisterClientInitScript(
                "deframe",
                String.Format(
                    "if(top.location != window.location) top.location = '{0}?ReturnUrl={1}';",
                    FormsAuthentication.LoginUrl,
                    Server.UrlEncode(DefaultUrl))
            );
        }
        #endregion

        #endregion

        #region Private

        #region FindControl
        private Ext.Net.ResourceManager FindScriptManager()
        {
            return FindControl(null, typeof(Ext.Net.ResourceManager)) as Ext.Net.ResourceManager;
        }
        private Control FindControl(Control parent, Type type)
        {
            ControlCollection controls = parent == null ? Controls : parent.Controls;

            foreach (Control ctrl in controls)
            {
                if (ctrl.GetType() == type)
                {
                    return ctrl;
                }

                Control sc = FindControl(ctrl, type);

                if (sc != null)
                {
                    return sc;
                }
            }

            return null;
        }
        #endregion

        #endregion

        #endregion
    }
}
